﻿Imports System.Data.SqlClient

Public Class frmProspectsCasesStatus

    Public SNo As Integer

    Sub FillProspectsLossReasons()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select Distinct Reason From ProspectsLossReasons", cnn)
            Dim Reader As SqlDataReader

            Me.CombLossReason.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.CombLossReason.Items.Add(Reader.Item(0))
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub UpdateStatus()
        If Me.RLost.Checked = True And Me.CombLossReason.SelectedIndex = -1 Then
            Me.ErrProv.SetError(Me.CombLossReason, "Please select loss reason")
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand
                Dim Trans As SqlTransaction

                cnn.Open()
                Trans = cnn.BeginTransaction
                cmd.Connection = cnn
                cmd.Transaction = Trans

                cmd.CommandText = "Update ProspectCases Set Status=@Status,LossReason=@LossReason Where SNo=" & SNo

                'Add values

                If RActive.Checked = True Then
                    cmd.Parameters.AddWithValue("@Status", "Active")

                ElseIf RadReady.Checked = True Then
                    cmd.Parameters.AddWithValue("@Status", "Ready")

                ElseIf RadRegistered.Checked = True Then
                    cmd.Parameters.AddWithValue("@Status", "Registered")

                ElseIf RWon.Checked = True Then
                    cmd.Parameters.AddWithValue("@Status", "Won")

                ElseIf RadConfirmed.Checked = True Then
                    cmd.Parameters.AddWithValue("@Status", "Firm Order")

                ElseIf RLost.Checked = True Then
                    cmd.Parameters.AddWithValue("@Status", "Lost")

                ElseIf RNonDesired.Checked = True Then
                    cmd.Parameters.AddWithValue("@Status", "Non Desired")
                End If
                cmd.Parameters.AddWithValue("@LossReason", Me.CombLossReason.Text)

                cmd.ExecuteNonQuery()

                Trans.Commit()
                cnn.Close()

                If RActive.Checked = True Then
                    Me.txtStatus.BackColor = Color.Yellow
                    Me.txtStatus.Text = "Active"

                ElseIf RadReady.Checked = True Then
                    Me.txtStatus.BackColor = Color.Green
                    Me.txtStatus.Text = "Ready"

                ElseIf RWon.Checked = True Then
                    Me.txtStatus.BackColor = Color.GreenYellow
                    Me.txtStatus.Text = "Won"

                ElseIf RadConfirmed.Checked = True Then
                    Me.txtStatus.BackColor = Color.Purple
                    Me.txtStatus.Text = "Firm Order"

                ElseIf RadRegistered.Checked = True Then
                    Me.txtStatus.BackColor = Color.White
                    Me.txtStatus.Text = "Registered"

                ElseIf RLost.Checked = True Then
                    Me.txtStatus.BackColor = Color.Red
                    Me.txtStatus.Text = "Lost"

                ElseIf RNonDesired.Checked = True Then
                    Me.txtStatus.BackColor = Color.Silver
                    Me.txtStatus.Text = "Non Desired"
                End If

                If Me.RLost.Checked = True Then
                    Me.CombLossReason.Enabled = True

                ElseIf Me.RLost.Checked = False Then
                    Me.CombLossReason.Enabled = False
                    Me.CombLossReason.SelectedIndex = -1
                End If

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub frmProspectsCasesStatus_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FillProspectsLossReasons()

        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From ProspectCases Where SNo=" & SNo, cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)

                Me.txtStatus.Text = Reader.Item("Status")

            End While
            cnn.Close()

            'Check status
            If Me.txtStatus.Text = "Active" Then
                Me.txtStatus.BackColor = Color.Yellow

            ElseIf Me.txtStatus.Text = "Won" Then
                Me.txtStatus.BackColor = Color.GreenYellow

            ElseIf Me.txtStatus.Text = "Firm Order" Then
                Me.txtStatus.BackColor = Color.Purple

            ElseIf Me.txtStatus.Text = "Registered" Then
                Me.txtStatus.BackColor = Color.White

            ElseIf Me.txtStatus.Text = "Lost" Then
                Me.txtStatus.BackColor = Color.Red

            ElseIf Me.txtStatus.Text = "Non Desired" Then
                Me.txtStatus.BackColor = Color.Silver
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        UpdateStatus()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New frmProspectLossReasons
        a.ShowDialog()
        FillProspectsLossReasons()
    End Sub
End Class